<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Utils
{

    /**
     * @return string
     */
    public function getCurrentDayString()
    {
        $now = getdate();
        return $current_time = $now["year"] . "-" . $now["mon"] . "-" . $now["mday"] . " " . $now["hours"] . ":" . $now["minutes"] . ":" . $now["seconds"];
    }

    /**
     * @param $require
     * @return bool
     */
    public function check_isset($require)
    {
        foreach ($require as $fieldname) {
            if (!isset($_POST[$fieldname])) {
                return false;
            }
        }
        return true;
    }

    /**
     * @param $require
     * @return bool
     */
    public function check_isset_get($require)
    {
        foreach ($require as $fieldname) {
            if (!isset($_GET[$fieldname])) {
                return false;
            }
        }
        return true;
    }

    /**
     * @param $require
     * @return bool
     */
    public function check_empty($require)
    {
        foreach ($require as $fieldname) {
            if (empty($_POST[$fieldname])) {
                return false;
            }
        }
        return true;
    }

    /**
     * @param $require
     * @return bool
     */
    public function check_empty_get($require)
    {
        foreach ($require as $fieldname) {
            if (empty($_GET[$fieldname])) {
                return false;
            }
        }
        return true;
    }

    /**
     * @param $email
     * @return bool
     */
    public function check_valid_email($email)
    {
        $pattern = '/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/';
        if (preg_match($pattern, $email) == 0)
            return false;
        else
            return true;
    }

    /**
     * @param $str
     * @return string
     */
    public function add_single_quote($str)
    {
        return "'" . $str . "'";
    }

    /**
     * @param $str
     * @param int $isComma
     * @return string
     */
    function format($str, $isComma = 1)
    {
        // 1 as visible comma, 0 as not comma
        // default as 1
        if ($isComma == 1) {
            return "'" . $str . "',";
        } else
            return "'" . $str . "'";
    }

    /**
     * @param $str
     * @return bool|mixed
     */
    public function strip_unicode($str)
    {
        if (!$str)
            return false;
        $unicode = array('a' => 'á|à|ả|ã|ạ|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ', 'A' => 'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ằ|Ẳ|Ẵ|Ặ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ', 'd' => 'đ', 'D' => 'Đ', 'e' => 'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ', 'E' => 'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ', 'i' => 'í|ì|ỉ|ĩ|ị', 'I' => 'Í|Ì|Ỉ|Ĩ|Ị', 'o' => 'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ', 'O' => 'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ', 'u' => 'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự', 'U' => 'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự', 'y' => 'ý|ỳ|ỷ|ỹ|ỵ', 'Y' => 'Ý|Ỳ|Ỷ|Ỹ|Ỵ');
        foreach ($unicode as $khongdau => $codau) {
            $arr = explode("|", $codau);
            $str = str_replace($arr, $khongdau, $str);
        }
        return $str;
    }

    /**
     * @param $str
     * @return mixed
     */
    public function change_title($str)
    {
        $str = $this->stripUnicode($str);
        $str = str_replace("?", "", $str);
        $str = str_replace("&", "", $str);
        $str = str_replace("'", "", $str);
        $str = str_replace("  ", " ", $str);
        $str = trim($str);
        $str = mb_convert_case($str, MB_CASE_TITLE, 'utf-8');
        // MB_CASE_UPPER/MB_CASE_TITLE/MB_CASE_LOWER
        $str = str_replace(" ", "-", $str);
        return $str;
    }

    /**
     * @param $text
     * @return string
     */
    public function chuanhoa($text)
    {
        $text = trim($text);
        while (strchr($text, "  ") != "")
            $text = str_replace("  ", " ", $text);
        return $text;
    }

    /**
     * @param $str
     * @return mixed
     */
    public function bokytudacbiet($str)
    {
        $unicode = array('`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', '{', ']', '}', '\\', '|', ';', ':', '\'', '"', ',', '<', '.', '>', '/', '?');
        foreach ($unicode as $uni)
            while (strchr($str, $uni) != "")
                $str = str_replace($uni, "", $str);
        return $str;
    }

    public function chuthuong($str)
    {
        if (!$str)
            return false;
        $unicode = array('q' => 'Q', 'w' => 'W', 'e' => 'E',
                         'é' => 'É', 'è' => 'È', 'ẻ' => 'Ẻ',
                         'ẽ' => 'Ẽ', 'ẹ' => 'Ẹ', 'ê' => 'Ê',
                         'ế' => 'Ế', 'ề' => 'Ề', 'ể' => 'Ể',
                         'ễ' => 'Ễ', 'ệ' => 'Ệ', 'r' => 'R',
                         't' => 'T', 'y' => 'Y', 'ý' => 'Ý',
                         'ỳ' => 'Ỳ', 'ỷ' => 'Ỷ', 'ỹ' => 'Ỹ',
                         'ỵ' => 'Ỵ', 'u' => 'U', 'ú' => 'Ú',
                         'ù' => 'Ù', 'ủ' => 'Ủ', 'ũ' => 'Ũ',
                         'ụ' => 'Ụ', 'ư' => 'Ư', 'ứ' => 'Ứ',
                         'ừ' => 'Ừ', 'ử' => 'Ử', 'ữ' => 'Ữ',
                         'ự' => 'Ự', 'i' => 'I', 'í' => 'Í',
                         'ì' => 'Ì', 'ỉ' => 'Ỉ', 'ĩ' => 'Ĩ',
                         'ị' => 'Ị', 'o' => 'O', 'ó' => 'Ó',
                         'ò' => 'Ò', 'ỏ' => 'Ỏ', 'õ' => 'Õ',
                         'ọ' => 'Ọ', 'ô' => 'Ô', 'ố' => 'Ố',
                         'ồ' => 'Ồ', 'ổ' => 'Ổ', 'ỗ' => 'Ỗ',
                         'ộ' => 'Ộ', 'ơ' => 'Ơ', 'ớ' => 'Ớ',
                         'ờ' => 'Ờ', 'ở' => 'Ở', 'ỡ' => 'Ỡ',
                         'ợ' => 'Ợ', 'p' => 'P', 'a' => 'A',
                         'á' => 'Á', 'à' => 'À', 'ả' => 'Ả',
                         'ã' => 'Ã', 'ạ' => 'Ạ', 'ă' => 'Ă',
                         'ắ' => 'Ắ', 'ằ' => 'Ằ', 'ẳ' => 'Ẳ',
                         'ẵ' => 'Ẵ', 'ặ' => 'Ặ', 'â' => 'Â',
                         'ấ' => 'Ấ', 'ầ' => 'Ầ', 'ẩ' => 'Ẩ',
                         'ẫ' => 'Ẫ', 'ậ' => 'Ậ', 's' => 'S',
                         'd' => 'D', 'đ' => 'Đ', 'f' => 'F',
                         'g' => 'G', 'h' => 'H', 'j' => 'J',
                         'k' => 'K', 'l' => 'L', 'z' => 'Z',
                         'x' => 'X', 'c' => 'C', 'v' => 'V',
                         'b' => 'B', 'n' => 'N', 'm' => 'M',);
        foreach ($unicode as $nonUnicode => $uni)
            $str = preg_replace("/($uni)/i", $nonUnicode, $str);
        return $str;
    }

    /**
     * @param $text
     * @param $char
     * @return string
     */
    public function replace_space($text, $char)
    {
        $text = trim($text);
        while (strchr($text, " ") != "")
            $text = str_replace(" ", $char, $text);
        return $text;
    }

    /**
     * @param $status
     * @return string
     */
    public function create_status_record($status)
    {
        if ($status == 1)
            return "<img src='resources/images/ball_green.png' alt='green ball'>";
        if ($status == 0)
            return "<img src='resources/images/ball_red.png' alt='red ball'>";
    }

    /**
     * @param $Str_Location
     * @param int $Bln_Replace
     * @param null $Int_HRC
     */
    function redirect($Str_Location, $Bln_Replace = 1, $Int_HRC = NULL)
    {
        if (!headers_sent()) {
            header('location: ' . urldecode($Str_Location), $Bln_Replace, $Int_HRC);
            exit;
        }

        exit('<meta http-equiv="refresh" content="0; url=' . urldecode($Str_Location) . '"/>');
        # | exit('<script>document.location.href=' . urldecode($Str_Location) . ';</script>');
        return;
    }

    /**
     * @return string
     */
    function curPageURL()
    {
        $pageURL = 'http';
        if ($_SERVER["HTTPS"] == "on") {
            $pageURL .= "s";
        }
        $pageURL .= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
            $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
        } else {
            $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
        }
        return $pageURL;
    }

    /**
     * Return URL-Friendly string slug
     * @param string $string
     * @return string
     */

    function seoUrl($string)
    {
        $string = $this->strip_unicode($string);
        //Unwanted:  {UPPERCASE} ; / ? : @ & = + $ , . ! ~ * ' ( )
        $string = strtolower($string);
        //Strip any unwanted characters
        $string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
        //Clean multiple dashes or whitespaces
        $string = preg_replace("/[\s-]+/", " ", $string);
        //Convert whitespaces and underscore to dash
        $string = preg_replace("/[\s_]/", "-", $string);
        return $string;
    }

    /**
     * @param $number
     * @param bool $fractional
     * @return mixed|string
     */
	function formatMoney($number, $fractional = false)
	{
		if ($fractional) {
			$number = sprintf('%.2f', $number);
		}
		while (true) {
			$replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
			if ($replaced != $number) {
				$number = $replaced;
			} else {
				break;
			}
		}
		return $number;
	}

    /**
     * @param $str
     * @return mixed
     */
    function getUrlThumbnail($str)
    {
        return str_replace($search = '/userupload/images/', $replace = '/userupload/_thumbs/Images/', $str);
    }

	/**
	 * Navigates through an array and removes slashes from the values.
	 *
	 * If an array is passed, the array_map() function causes a callback to pass the
	 * value back to the function. The slashes from this value will removed.
	 *
	 * @since 2.0.0
	 *
	 * @param array|string $value The array or string to be stripped.
	 * @return array|string Stripped array (or string in the callback).
	 */
	function stripslashes_deep($value) {
		if ( is_array($value) ) {
			$value = array_map('stripslashes_deep', $value);
		} elseif ( is_object($value) ) {
			$vars = get_object_vars( $value );
			foreach ($vars as $key=>$data) {
				$value->{$key} = stripslashes_deep( $data );
			}
		} else {
			$value = stripslashes($value);
		}

		return $value;
	}

	/**
	 * Hàm trả về chuổi an toàn khi nội dung chuỗi đã bi mã hóa trên javascript bởi hàm encodeURIComponent(string)
	 *
	 * @param $str
	 * @return string
	 */
	public function safeStringDecode($str) {
		$str = urldecode($str);
		$str = $this->stripslashes_deep($str);
		$str = mysql_real_escape_string($str);
		return $str;
	}
	/**
	 * Hàm trả về chuổi an toàn khi insert vào database.
	 * Dùng cho các chuổi không có encode trên javascript
	 * @param $str
	 * @return string
	 */
	public function safeString($str) {
		$str = $this->stripslashes_deep($str);
		$str = mysql_real_escape_string($str);
		return $str;
	}
}

?>
